﻿using HighTimer;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace LodingWait
{
    /// <summary>
    /// LoadingControl.xaml 的交互逻辑
    /// 载入效果 ： https://www.cnblogs.com/miketwais/articles/WPF_Loading.html
    /// 高精度定时器 ： https://download.csdn.net/download/jwh2004/9511414
    /// </summary>
    public partial class LoadingControl : UserControl
    {
        private readonly DispatcherTimer HideTimer;//用来隐藏用户界面(这里的功能放在HighTimer里会有子线程异常)
        private HighAccurateTimer AnimationTimer = new HighAccurateTimer(); //高精度定时器(效果很好，但是比较占用CPU资源)
        private int IntervalTime = 100;//间隔100毫秒
        private int ShowTime = 3 * 1000; //显示时间为3秒
        private int SurplusTime = 0;//剩余时间


        #region Constructor  
        public LoadingControl()
        {
            InitializeComponent();

            //动画定时器
            AnimationTimer.Interval = IntervalTime;

            //隐藏定时器
            HideTimer = new DispatcherTimer(DispatcherPriority.ContextIdle, Dispatcher)
            {
                Interval = new TimeSpan(0, 0, 0, 0, 100)
            };
        }
        #endregion

        #region Private Methods  
        private void Start()
        {
            //励志
            this.LiZhi.Text = new LiZhi().GetLook();

            //动画
            SurplusTime = ShowTime;
            AnimationTimer.Enabled = true;
            AnimationTimer.Elapsed += AnimationTick;

            //开始隐藏定时器
            HideTimer.Tick += HideTick;
            HideTimer.Start();
        }

        private void Stop()
        {
            //停止动画
            AnimationTimer.Enabled = false;
            AnimationTimer.Elapsed -= AnimationTick;

            //停止隐藏定时器
            HideTimer.Tick += HideTick;
            HideTimer.Start();
        }

        private void AnimationTick(object sender, TimerEventArgs e)
        {
            //更新剩余时间
            if (SurplusTime == 0)
                return;
            else
                SurplusTime -= IntervalTime;

            Action AnimationAction = () =>
            {
                this.Background.Opacity = SurplusTime * 1.00 / ShowTime;//改变透明度
                this.SpinnerRotate.Angle = (SpinnerRotate.Angle + 36) % 360;
            };
            this.SpinnerRotate.Dispatcher.Invoke(AnimationAction);
        }

        private void HideTick(object sender, EventArgs e)
        {
            if (SurplusTime == 0)
            {
                this.Visibility = Visibility.Collapsed;
            }
        }

        private void HandleLoaded(object sender, RoutedEventArgs e)
        {
            const double offset = Math.PI;
            const double step = Math.PI * 2 / 10.0;

            SetPosition(C0, offset, 0.0, step);
            SetPosition(C1, offset, 1.0, step);
            SetPosition(C2, offset, 2.0, step);
            SetPosition(C3, offset, 3.0, step);
            SetPosition(C4, offset, 4.0, step);
            SetPosition(C5, offset, 5.0, step);
            SetPosition(C6, offset, 6.0, step);
            SetPosition(C7, offset, 7.0, step);
            SetPosition(C8, offset, 8.0, step);
        }

        private void SetPosition(Ellipse ellipse, double offset, double posOffSet, double step)
        {
            ellipse.SetValue(Canvas.LeftProperty, 50.0 + Math.Sin(offset + posOffSet * step) * 50.0);
            ellipse.SetValue(Canvas.TopProperty, 50 + Math.Cos(offset + posOffSet * step) * 50.0);
        }

        private void HandleUnloaded(object sender, RoutedEventArgs e)
        {
            Stop();
        }

        private void HandleVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            bool isVisible = (bool)e.NewValue;

            if (isVisible)
                Start();
            else
                Stop();
        }
        #endregion

    }
}








